angular.module('admin.index.controllers', [])
    .controller('index-ctrl', function ($scope, adService, cacheService) {
        /*$scope.auth = {};
        adService.getWithCBLoading("../../json/auth.json", null, function(res){
            cacheService.setObject("auth", res);
            $scope.auth = res;
        }, function(res){
            cacheService.setObject("auth", res);
            $scope.auth = res;
        })*/
    })
    .controller('balance-ctrl', function ($scope, adService, cacheService, $http) {
        $scope.model = {
            accountId: '',
            account: '',
            auth: '',
            bank: 'ICBC',
            authorized: 0,
            balance: null
        };
        $scope.page = 1;
        $scope.totalBalances = 0;
        $scope.models = [];
        $scope.models.push(angular.copy($scope.model));

        $scope.addMore = function () {
            $scope.models.push(angular.copy($scope.model));
        };
        $scope.remove = function (idx) {
            $scope.models.splice(idx, 1);
        };

        $scope.authorize = function (model) {
            if (!model.account) {
                showTip("please input account");
                return ;
            }
            if (!model.auth) {
                showTip("please input authorization key");
                return ;
            }
            var url = 'api/accounts/identification/' + model.account;
            $http({
                url: url,
                params: {},
                method: "GET",
                timeout: 10000,
                headers: {
                    "financialId": "f1",
                    "scheme": model.bank
                }
            }).then(function successCallBack(response) {
                var res = response.data;
                if (res.code == 0 && res.data) {
                    model.authorized = 1;
                    model.accountId = res.data.accountId;
                } else {
                    model.authorized = 2;
                }
            }, function errorCallBack(response) {
            })
        };

        $scope.calculate = function () {
            $scope.totalBalances = 0;
            for (i = 0; i < $scope.models.length; i++) {
                if ($scope.models[i] && $scope.models[i].accountId) {
                    $http({
                        url: 'api/balances/' + $scope.models[i].accountId,
                        params: {},
                        method: "GET",
                        timeout: 10000,
                        headers: {
                            "financialId": "f1"
                        }
                    }).then(function successCallBack(response) {
                        var res = response.data;
                        if (res.code == 0 && res.data && res.data.length > 0) {
                            for (j = 0; j < $scope.models.length; j++) {
                                if ($scope.models[j] && $scope.models[j].accountId == res.data[0].accountId) {
                                    $scope.models[j].balance = res.data[0].amount;
                                }
                            }
                            $scope.totalBalances += parseFloat(res.data[0].amount.amount);
                        }
                    }, function errorCallBack(response) {
                    });
                }
            }
            $scope.page = 2;
        };
    })
;